<?php

namespace backend\controllers;
use backend\controllers\BaseController;
use Yii;
use yii\web\Controller;
class AdminController extends BaseController{
	public $layout = 'common';
	// public function init()
	// {
	// 	parent::init();
	// }
	public function actionAdd()
	{

		// $controllerID = Yii::$app->controller->id;
		// $actionID = Yii::$app->controller->action->id;
		// $nodes = $controllerID.'/'.$actionID;
		// die;
		if(Yii::$app->request->isPost){
			$admin = Yii::$app->request->post();
			$pwd   = md5($admin['admin_pwd']);
			Yii::$app->db->createCommand()->insert('e_admin', [
									    'admin_name' => $admin['admin_name'],
									    'admin_pwd' => $pwd,
									])->execute();
			$admin_id = Yii::$app->db->getLastInsertID();
			if($admin_id){
				foreach($admin['role_id'] as $k => $v){
					Yii::$app->db->createCommand()->insert('e_admin_role', [
									    'admin_id' => $admin_id,
									    'role_id' => $v,
									])->execute();
				}
			}
			$this->refresh();
		}

		$role = Yii::$app->db->createCommand('SELECT * FROM e_role')->queryAll();
		$admins = Yii::$app->db->createCommand('SELECT * FROM e_admin')
            ->queryAll();
            foreach($admins as $key => $val){
            	$roles = Yii::$app->db->createCommand("select * from e_role where role_id in (select role_id from e_admin_role where admin_id = ".$val['admin_id'].")")
            		->queryAll();
            		$admins[$key]['roles'] = $roles;
            }
		return $this->render('add',['roles'=>$role,'admins'=>$admins]);
	}
}